/*
 * Created on Feb 19, 2006
 *
 */
package com.fourtwoeight.controllers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;




/**
 * @author simona
 */

public class DisplayImage extends DisplayThumbs {
	

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		logger = LogFactory.getLog(this.getClass());
		
		long start = System.currentTimeMillis();

		// Get the base image directory
		baseImageDirectory = getBaseDirectory();

		// If no base image dir..bad thing
		if (baseImageDirectory == null) {
			request.setAttribute(WebConstants.ERROR_MESSAGE,
					"No base directory found");
			return mapping.findForward(WebConstants.FAILURE);
		}

		String currentFilename = request.getParameter(WebConstants.FILE);

		if (currentFilename == null) {
			currentFilename = (String) request.getSession().getAttribute(
					WebConstants.FILE);
		}

		if (currentFilename == null) {
			return mapping.findForward(WebConstants.FAILURE);
		}

		currentFilename = baseImageDirectory + "/" + currentFilename;

		File currentFile = new File(currentFilename);
		request.setAttribute(WebConstants.FILE, currentFile);

		currentBrowsedDirectory = subtractString(currentFile.getParent(),baseImageDirectory);

		currentBrowsedDirectory = currentBrowsedDirectory.replace("\\", "/");
		
		//logger.debug("Current browsed directory: " + currentBrowsedDirectory);
		
		
		// find position within directory listing
		File[] files = new File(currentFile.getParent()).listFiles();

		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			if (file.equals(currentFile)) {
				String previousImageFilename = null;
				String nextImageFilename = null;

				
				if (i > 0) {
					File previousImageFile = files[i - 1];
					if (previousImageFile != null
							&& !previousImageFile.isDirectory()) {
						previousImageFilename = previousImageFile.getName();
					}
					if (previousImageFilename != null) {
						request.setAttribute(WebConstants.PREVIOUS, "/"
								+ previousImageFilename);
					}
				}				
				if (i != files.length -1) {
					File nextImageFile = files[i + 1];
					if (nextImageFile != null && !nextImageFile.isDirectory()) {
						nextImageFilename = nextImageFile.getName();
					}
					if (nextImageFilename != null) {
						request.setAttribute(WebConstants.NEXT, "/"
								+ nextImageFilename);
					}
				}				
				
			}
		}

		request.getSession().setAttribute(WebConstants.CURRENT_DIRECTORY, currentBrowsedDirectory);

		processThumbnail(currentFile, "slides", 640, 640);

		// get image comments if they exist
		String imageNotes = "No notes for this image";
		String imageNotesRaw = "";

		File notesFile = new File(currentFile.getParent() + File.separatorChar
				+ "_notes" + File.separatorChar + currentFile.getName()
				+ ".notes");

		if (notesFile.exists()) {
			StringBuffer sb = new StringBuffer();
			StringBuffer sbRaw = new StringBuffer();
			BufferedReader input = null;

			try {
				input = new BufferedReader(new FileReader(notesFile));
				String line = null;
				while ((line = input.readLine()) != null) {
					sb.append(line);
					sbRaw.append(line);
					sb.append("<br>");
					sbRaw.append("\n");
				}
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
			} catch (IOException ex) {
				ex.printStackTrace();
			} finally {
				try {
					if (input != null) {
						input.close();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
			imageNotes = sb.toString();
			imageNotesRaw = sbRaw.toString();
		}

		request.setAttribute(WebConstants.IMAGE_NOTES, imageNotes);
		request.setAttribute(WebConstants.IMAGE_NOTES_RAW, imageNotesRaw);
		request.setAttribute(WebConstants.IMAGE_NOTES_FILENAME, notesFile);
		request.setAttribute(WebConstants.IMAGE_DIRECTORY_URL,
				getImageDirectoryURL(request));

		logger.debug(currentFile.getName() + " processed in total, in " + (System.currentTimeMillis() - start) + "ms");
		
		return mapping.findForward(WebConstants.SUCCESS);
	}
}
